What is SQL?
Relational databases
Select query for a specific columns
SELECT column, another_column, …
FROM mytable;
Select query for all columns
SELECT *
FROM mytable;
Select query with constraints
SELECT column, another_column, …
FROM mytable
WHERE condition
AND/OR another_condition
AND/OR …;
Select query with unique results
SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);
Select query with ordered results
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;
Select query with limited rows
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
Lesson 13: Inserting rows
Inserting new data
Insert statement with values for all columns
INSERT INTO mytable
VALUES (value_or_expr, another_value_or_expr, …),
(value_or_expr_2, another_value_or_expr_2, …),
…;
```
Update statement with values
UPDATE mytable
SET column = value_or_expr,
other_column = another_value_or_expr,
…
WHERE condition;
Delete statement with condition
DELETE FROM mytable
WHERE condition;
Create table statement w/ optional table constraint and default value
CREATE TABLE IF NOT EXISTS mytable (
column DataType TableConstraint DEFAULT default_value,
another_column DataType TableConstraint DEFAULT default_value,
…
);
Table data types
Table constraints
Movies table schema
CREATE TABLE movies (
id INTEGER PRIMARY KEY,
title TEXT,
director TEXT,
year INTEGER,
length_minutes INTEGER
);
Altering table to add new column(s)
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
DEFAULT default_value;
Altering table to remove column(s)
ALTER TABLE mytable
DROP column_to_be_deleted;
Altering table name
ALTER TABLE mytable
RENAME TO new_table_name;
Drop table statement
DROP TABLE IF EXISTS mytable;